# _*_ coding:utf-8 _*_
"""
__Author__    :  Icy
__Date__      :  2018/5/4
__File__      :  utils.py
__Desc__      :
"""
import hashlib

import tornado.web
from optsql.model import DB
def encry_passwd(passwd):
	m = hashlib.md5()
	m.update(passwd.encode('utf8'))
	return m.digest()
class AuthBackend(object):
	@staticmethod
	def authticate(username, passwd):
		if DB.check_user(**{'username': username,
		                    'passwd': encry_passwd(passwd)}):
			return True
		return False

	@staticmethod
	def login(handler):
		"""
		:param handler: type -> loginHandler
		:return:
		"""

		handler.set_secure_cookie('username', handler.get_argument('username'))

